﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.ComponentModel;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace InHD.model
{
    class Package : IDataErrorInfo, INotifyPropertyChanged
    {
        #region Creation

        public static Package CreatePackage()
        {
            return new Package();
        }

        public static Package CreatePackageInfo(
            string pacUNK,
            int quantity,
            double salePrice,
            string transUNK,
            string proUNK,
            string proName,
            string proSN)
        {
            return new Package
            {
                PackageUNK = pacUNK,
                Quantity = quantity,
                UnitPrice = salePrice,
                TransactionUNK = transUNK,
                ProductUNK = proUNK,
                ProductName = proName,
                ProductSN = proSN
            };
        }

        public static Package CreateBusinessPackage(
                string serial,
                string desc,
                string catname,
                double price,
                int quantity,
                string productName)
        {
            return new Package
            {
                SerialNo = serial,
                Description = desc,
                CategoryName = catname,
                UnitPrice =  price,
                Quantity= quantity,
                ProductName = productName
            };
        }

        protected Package()
        {
        }

        #endregion // Creation

        #region State Properties

        public string PackageUNK { get; set; }

        //public int Quantity { get; set; }

        public double SalePrice { get; set; }

        public string TransactionUNK { get; set; }

        public string ProductUNK { get; set; }

        private string _productName;
        public string ProductName
        {
            get
            {
                return this._productName;
            }
            set
            {
                this._productName = value;
                OnPropertyChanged("ProductName");
            }
        }

        public string ProductSN { get; set; }

        #endregion // State Properties

        #region Fields for business code
        private int _quantity;
        public string SerialNo { get; set; }
        public string Description { get; set; }
        public double UnitPrice
        {
            get;
            set;
        }

        public int Quantity
        {
            get
            {
                return this._quantity;
            }
            set
            {
                this._quantity = value;
                OnPropertyChanged("Quantity");
                OnPropertyChanged("PackagePrice");
            }
        }

        public double PackagePrice
        {
            get
            {
                return UnitPrice * Quantity; 
            }
        }

        public string CategoryName { get; set; }

        public double SaveImportPrice { get; set; }
        public double SaveWholesale { get; set; }
        public double SaveRetail { get; set; }
        #endregion

        #region Validation

        /// <summary>
        /// Returns true if this object has no validation errors.
        /// </summary>
        public bool IsValid
        {
            get
            {
                foreach (string property in ValidatedProperties)
                    if (GetValidationError(property) != null)
                        return false;

                return true;
            }
        }

        static readonly string[] ValidatedProperties = 
        { 
            "UnitPrice", 
            "Quantity"
        };

        string GetValidationError(string propertyName)
        {
            if (Array.IndexOf(ValidatedProperties, propertyName) < 0)
                return null;

            string error = null;

            switch (propertyName)
            {
                case "UnitPrice":
                    error = this.ValidateDouble();
                    break;

                case "Quantity":
                    error = this.ValidateInteger();
                    break;

                default:
                    Debug.Fail("Unexpected property being validated on CompanyInfo: " + propertyName);
                    break;
            }

            return error;
        }

        private string ValidateDouble()
        {
            
            return null;
        }

        private string ValidateInteger()
        {
            
            return null;
        }

        
        static bool IsValidInteger(string number)
        {
            // This regex pattern came from: http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx
            string pattern = @"^[0-9]$";

            return Regex.IsMatch(number, pattern, RegexOptions.IgnoreCase);
        }

        static bool IsValidReal(string number)
        {
            // This regex pattern came from: http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx
            string pattern = @"^[0-9]+\.?[0-9]*$";

            return Regex.IsMatch(number, pattern, RegexOptions.IgnoreCase);
        }

        #endregion // Validation

        string IDataErrorInfo.Error { get { return null; } }

        string IDataErrorInfo.this[string propertyName]
        {
            get { return this.GetValidationError(propertyName); }
        }

        private void OnPropertyChanged(string propertyName)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged = delegate { };
    }
}
